//
//  SpecialTableViewController.m
//  BJS151148_ TeamProject_Travel
//
//  Created by lanou3g on 16/3/22.
//  Copyright © 2016年 lanou3g. All rights reserved.
//


/**专题页面*/


#import "SpecialTableViewController.h"
#import "CDHttpHelper.h"
#import "specialModel.h"
#import "SpecialCell.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "DetailsSpecialTableViewController.h"
#import "Singleton.h"

static NSString *identifier = @"Special";
static NSInteger page = 1;

@interface SpecialTableViewController ()
@property(nonatomic,strong)NSMutableArray *dataArray; // 解析数组
@end

@implementation SpecialTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.allDataArray = [NSMutableArray array];
    [self.tableView registerNib:[UINib nibWithNibName:@"SpecialCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:identifier];
    
    
    [self handleData:1];
    
    // 上拉,下拉刷新
    [self updateData];
}

- (void)updateData
{
    // 页面上方,下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.allDataArray removeAllObjects];
            [self handleData:1];
            [self.tableView.mj_header endRefreshing];
        });
    }];
    
    // 页面下方,上拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self handleData:++page];
        __weak typeof(self) pSelf = self;
        pSelf.passBlock = ^ (NSMutableArray *arr) {
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
            
        };
    }];
}

- (void)handleData:(NSInteger)leafs
{
    self.dataArray = [NSMutableArray array];
    [[CDHttpHelper defaultCDHttpHelper]get:[NSString stringWithFormat:@"https://chanyouji.com/api/articles.json?page=%ld",leafs]params:nil success:^(id responseObj) {
        NSError *error = nil;
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableLeaves error:&error];
        
        for (NSDictionary *dict in dictionary) {
            specialModel *model = [[specialModel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataArray addObject:model];
        }
        
        if (leafs > 1) {
            // 用block把解析的数据dataArray传到控制器
            __weak typeof(self) pSelf = self;
            
            if (pSelf) {
                pSelf.passBlock(self.dataArray);
            }
        }
        [self.allDataArray addObjectsFromArray:self.dataArray];
        if (leafs == 1) {
            [self.allDataArray removeObjectAtIndex:0];
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}



#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.allDataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SpecialCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier ];
    if (cell == nil) {
        cell = [[SpecialCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    specialModel *model = [[specialModel alloc]init];
    model = self.allDataArray[indexPath.row];
    [cell.image_url sd_setImageWithURL:[NSURL URLWithString:model.image_url]];
    cell.name.text = model.name;
    cell.name.textColor = [UIColor whiteColor];
    cell.title.text = model.title;
    cell.title.textColor = [UIColor whiteColor];
    
    cell.image_url.layer.cornerRadius = 5;
    cell.image_url.layer.masksToBounds = YES;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 250;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsSpecialTableViewController *DetailsSpecialVC = [[DetailsSpecialTableViewController alloc]init];
    
    specialModel *model = [[specialModel alloc]init];
    model = self.allDataArray[indexPath.row];
    
    Singleton *single = [Singleton shareSingle];
    single.specialID = model.specialID;
    
    [self.navigationController pushViewController:DetailsSpecialVC animated:YES];
}




@end
